// 方式一
// 时间：80ms - 99%
function reverse(x) {
  let prev = 0,
    next = 0;
  while (x !== 0) {
    prev = x % 10;
    x = (x - prev) / 10;
    next = next * 10 + prev;
    if (next > 2 ** 31 - 1 || next < (-2) ** 31) {
      return 0;
    }
  }
  return next;
}

console.log(reverse(321));
